﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Assignment_2
{
    class Camera
    {
        #region Variables
        private Game _game;
        private Vector3 _position = new Vector3(0.0f, 0.0f, 200f);
        public Vector3 Position
        {
            get { return this._position; }
            set { this._position = value; }
        }
        private GameObject _target;
        public GameObject Target
        {
            get { return this._target; }
            set { _target = value; }
        }
        private Vector3 _upVector = Vector3.Up;
        public Vector3 Up
        {
            get { return this._upVector; }
            set { this._upVector = value; }
        }
        private Matrix _view;
        public Matrix View
        {
            get { return this._view; }
        }
        private Matrix _projection;
        public Matrix Projection
        {
            get { return this._projection; }
        }
        private Quaternion _rotation;
        public Quaternion Rotation { get { return this._rotation; } }
        float yDistance = 1.0f;
        float zDistance = 3.0f;
        #endregion

        public Camera(Game game, GameObject target)
        {
            this._game = game;
            this.yDistance = 0.2f;
            this.zDistance = .5f;
            this._position = new Vector3(0, yDistance, zDistance);//target.Position + new Vector3(0, yDistance, zDistance);
            this._target = target;
            InitCamera();
        }

        /// <summary>
        /// Initializes the camera.
        /// </summary>
        private void InitCamera()
        {
            this._projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                this._game.GraphicsDevice.DisplayMode.AspectRatio,
                .1f,
                1000.0f);

            this._view = Matrix.CreateLookAt(
                this._target.Position + this._position,
                _target.Position,
                _upVector);
        }

        public void Update(GameTime gameTime)
        {
            Vector3 campos = new Vector3(0, 0.1f, 0.6f);
            campos = Vector3.Transform(campos, Matrix.CreateFromQuaternion(Target.Rotation));
            campos += Target.Position;

            Vector3 camup = new Vector3(0, 1, 0);
            camup = Vector3.Transform(camup, Matrix.CreateFromQuaternion(Target.Rotation));

            _view = Matrix.CreateLookAt(campos, Target.Position, camup);
            _projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _game.GraphicsDevice.Viewport.AspectRatio, 0.2f, 500.0f);
        }
    }
}
